﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    AccesoDatos datos = new AccesoDatos();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        datos.LlenarDataset("Select Nombre, Graduacion from Articulos where CodTipo not like 'T005'","Articulos",ref ds);
        gvBebidas.DataSource = ds;
        gvBebidas.DataBind();
    }

    protected void gvBebidas_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Codigo;
        GridView gvNew = new GridView();
        datos.LlenarDataset("Select * from Articulos", "Articulos", ref ds);
        gvNew.DataSource = ds;
        gvNew.DataBind();
        Codigo = gvNew.Rows[gvBebidas.SelectedIndex].Cells[0].Text;
        lstIngredientes.Items.Clear();
        lstProductos.Items.Clear();
        SqlConnection connection = new SqlConnection();
        SqlCommand SQLcmd = new SqlCommand();
        connection.ConnectionString = "Data Source=localhost;Initial Catalog=Final;Integrated Security=True";
        SQLcmd.Connection = connection;
        SQLcmd.CommandType = CommandType.Text;
        SQLcmd.CommandText = "select distinct marcas.nombre from (articulos inner join artxmarca on artxmarca.codart = articulos.codart) inner join marcas on marcas.codmarca = artxmarca.codmarca WHERE articulos.nombre = " + "'" + Codigo + "'";
        connection.Open();
        SqlDataReader dr = SQLcmd.ExecuteReader();
        while (dr.Read())
        {
            lstIngredientes.Items.Add(dr[0].ToString().Trim());
        }
        connection.Close();  
    }
    protected void btnAgregar_Click(object sender, EventArgs e)
    {
        if (lstProductos.SelectedIndex != -1)
        {
            Dictionary<string, int> carrito;
            carrito = (Dictionary<string, int>)Session["Carrito"];
            if (carrito == null)
                carrito = new Dictionary<string, int>();
            string producto = gvBebidas.SelectedRow.Cells[1].Text.ToString() + ' ' + lstIngredientes.Items[lstIngredientes.SelectedIndex].ToString() + ' ' + lstProductos.Items[lstProductos.SelectedIndex].ToString();
            if (!carrito.ContainsKey(producto)) carrito.Add(producto, 1);
            else carrito[producto]++;
            Session["Carrito"] = carrito;
            this.Master.CargarCarrito();
            //string[] partes = new string[2];
            //partes= producto.Split('$'); //aca divido partes en 2 el string a partir del caracter
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "Mensaje", "<script type = \"text/javascript\">alert('" + partes[1] + "');</script>"); 
            
        }
    }
    protected void lstIngredientes_SelectedIndexChanged(object sender, EventArgs e)
    {
        lstProductos.Items.Clear();
        if (lstIngredientes.SelectedIndex != -1)
        {
            SqlConnection connection = new SqlConnection();
            SqlCommand SQLcmd = new SqlCommand();
            connection.ConnectionString = "Data Source=localhost;Initial Catalog=Final;Integrated Security=True";
            SQLcmd.Connection = connection;
            SQLcmd.CommandType = CommandType.Text;
            SQLcmd.CommandText = "select distinct artxmarca.tamaño, artxmarca.precio from artxmarca inner join marcas on marcas.codmarca = artxmarca.codmarca WHERE marcas.nombre = " + "'" + lstIngredientes.Items[lstIngredientes.SelectedIndex].ToString() + "'";
            connection.Open();
            SqlDataReader dr = SQLcmd.ExecuteReader();
            while (dr.Read())
            {
                lstProductos.Items.Add(dr[0].ToString().Trim() + "  $" + dr[1].ToString().Trim());
                //lstIngredientes.Items.Add(dr[0].ToString().Trim());
            }
            connection.Close(); 
        }
    }
}